//
//  LGPatientCTReportView.m
//  BreathTrainingTool
//
//  Created by 1234 on 2024/7/19.
//

#import "LGPatientCTReportView.h"
#import "LGScrollView.h"
#define topMargin    viewPix(20)

#define leftMargin   viewPix(40)+iPadMargin(10)

#define lineHeight   viewPix(35)

#define scrollWidth    (Screen_W - viewPix(83))

#define scrollHeight (lineHeight*5+viewPix(50))

@interface LGPatientCTReportView ()<UIScrollViewDelegate>
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *unitLabel;
/**   */
@property (nonatomic , strong)LGScrollView *scrollView;
/**   */
@property (nonatomic , strong)UIButton *typeBtn1;
/**   */
@property (nonatomic , strong)UIButton *typeBtn2;
/**   */
@property (nonatomic , strong)UIButton *typeBtn3;
/**   */
@property (nonatomic , assign)NSInteger totalCount;
/**   */
@property (nonatomic , assign)CGFloat itemWidth;
/**   */
@property (nonatomic , strong)NSDictionary *iconDic;
/**   */
@property (nonatomic , strong)NSDictionary *valueDic;
@end

@implementation LGPatientCTReportView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.itemWidth = scrollWidth/7.0;
        self.iconDic = @{@"1":@"患者报告-PGGN",@"2":@"患者报告-PGGN1",@"3":@"患者报告-PGGN2"};
        [self createSubView];
    }
    return self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    if ([dataDic isKindOfClass:[NSDictionary class]] && dataDic.allKeys.count>0) {
        self.totalCount = [dataDic[@"xAxis"] count]>0?[dataDic[@"xAxis"] count]:1;
        NSInteger count = (self.totalCount>7)?7:self.totalCount;
        self.itemWidth = scrollWidth/count;
        CGFloat totalWidth = viewPix(20)+self.itemWidth*self.totalCount;
        self.scrollView.contentSize = CGSizeMake(totalWidth, scrollHeight);
        [self.scrollView setContentOffset:CGPointMake(totalWidth-scrollWidth-viewPix(10), 0)];
        [self createXAxis:dataDic[@"xAxis"]];
    }
}
-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    NSMutableDictionary *dateDic = [NSMutableDictionary dictionary];
    for (NSInteger i=0; i<dataArray.count; i++) {
        NSDictionary *tempDic = dataArray[i];
        if ([tempDic[@"tuberType"] integerValue]>0) {
            NSString *dateStr = LGNSString(tempDic[@"markDate"]);
            NSMutableArray *dayArray = [dateDic.allKeys containsObject:dateStr]?[NSMutableArray arrayWithArray:dateDic[dateStr]]:[NSMutableArray array];
            [dayArray addObject:tempDic];
            [dateDic setValue:dayArray forKey:dateStr];
        }
    }
    NSArray *dayArray = dateDic.allKeys;
    self.totalCount = dayArray.count>0?dayArray.count:1;
    NSInteger count = (self.totalCount>7)?7:self.totalCount;
    self.itemWidth = scrollWidth/count;
    CGFloat totalWidth = viewPix(20)+self.itemWidth*self.totalCount;
    self.scrollView.contentSize = CGSizeMake(totalWidth, scrollHeight);
    [self.scrollView setContentOffset:CGPointMake(totalWidth-scrollWidth-viewPix(10), 0)];
    [self createXAxis:dayArray];
    for (NSInteger i=0 ; i<dayArray.count ; i++) {
        NSString *key = LGNSString(dayArray[i]);
        CGFloat pointX = viewPix(10)+self.itemWidth/2.0+self.itemWidth*i;
        NSArray *tempArray = dateDic[key];
        for (NSDictionary *tempDic in tempArray) {
            NSString *type = LGNSString(tempDic[@"tuberType"]);
            NSInteger value = [LGNSString(tempDic[@"tuberSize"]) integerValue]-1;
            value = (value<0)?0:value;
            CGFloat pointY = scrollHeight-(value*lineHeight+viewPix(50)+lineHeight*0.5);
            UIButton *button = [UIButton buttonWithImage:self.iconDic[type] selectImage:self.iconDic[type]];
            button.bounds = CGRectMake(0, 0, viewPix(20), viewPix(20));
            button.center = CGPointMake(pointX, pointY);
            button.tag = 1000+i;
//            [button addTarget:self action:@selector(pointButtonTouch:) forControlEvents:UIControlEventTouchUpInside];
            [self.scrollView addSubview:button];
//            NSValue *point = [NSValue valueWithCGPoint:button.center];
//            [pointArray addObject:point];
        }
    }
}


//创建X轴
-(void)createXAxis:(NSArray *)dayArray{
    for (NSInteger i=0; i<dayArray.count; i++) {
        NSString *dayString = [LGNSString(dayArray[i]) stringByReplacingOccurrencesOfString:@"-" withString:@"/"];
        UILabel *dayLabel = [UILabel lableWithText:dayString colorString:@"#333333" textFont:LGFont(12) textAlignment:NSTextAlignmentCenter lines:1];
        dayLabel.frame = CGRectMake(self.itemWidth*i, lineHeight*5, self.itemWidth, lineHeight);
        dayLabel.transform = CGAffineTransformRotate(dayLabel.transform, -M_PI_4);
        dayLabel.tag = 1000+i;
        [self.scrollView addSubview:dayLabel];
    }
}

-(void)createSubView{
    self.backgroundColor = [UIColor clearColor];
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.scrollView];
    [self.baseView addSubview:self.unitLabel];
    [self.baseView addSubview:self.typeBtn1];
    [self.baseView addSubview:self.typeBtn2];
    [self.baseView addSubview:self.typeBtn3];
    NSArray *titleArray = @[@"15以上",@"8-15",@"6-8",@"4-6",@"0-4"];
    for (NSInteger i=0; i<titleArray.count; i++) {
        //Y轴标题
        UILabel *label = [UILabel lableWithText:titleArray[i] colorString:@"#333333" textFont:LGFont(12) textAlignment:NSTextAlignmentCenter lines:1];
        label.frame = CGRectMake(0, topMargin+lineHeight*i, leftMargin, lineHeight);
        [self.baseView addSubview:label];
        //虚线
        LGDottedLineView *lineView = [[LGDottedLineView alloc]initWithFrame:CGRectMake(0, lineHeight*i, self.itemWidth*30+viewPix(50), lineHeight)];
        [self.scrollView addSubview:lineView];
    }
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), scrollHeight+topMargin+viewPix(35))];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 10;
    }
    return _baseView;
}


-(UILabel *)unitLabel{
    if(!_unitLabel){
        _unitLabel = [UILabel lableWithText:@"结节大小(mm)" colorString:@"#666666" textFont:LGFont(12) textAlignment:NSTextAlignmentCenter lines:1];
        _unitLabel.frame = CGRectMake(10, topMargin-10, 100, viewPix(17));
    }
    return _unitLabel;
}

-(LGScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[LGScrollView alloc] initWithFrame:CGRectMake(leftMargin, topMargin, scrollWidth, scrollHeight)];
        _scrollView.contentSize = CGSizeMake(self.itemWidth*30+viewPix(20), scrollHeight);
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

-(UIButton *)typeBtn1{
    if(!_typeBtn1){
        _typeBtn1 = [UIButton buttonWithTitle:@" PGGN" titleFont:LGFont(15) textColor:@"#333333" imageName:@"患者报告-PGGN"];
        _typeBtn1.frame = CGRectMake(0, kMaxY(self.scrollView.frame), (Screen_W-viewPix(32)-viewPix(30))/3.0, viewPix(20));
        _typeBtn1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    }
    return _typeBtn1;
}

-(UIButton *)typeBtn2{
    if(!_typeBtn2){
        _typeBtn2 = [UIButton buttonWithTitle:@" PSN" titleFont:LGFont(15) textColor:@"#333333" imageName:@"患者报告-PGGN1"];
        _typeBtn2.frame = CGRectMake(kMaxX(self.typeBtn1.frame)+viewPix(10), kMinY(self.typeBtn1.frame), (Screen_W-viewPix(32)-viewPix(30))/3.0, viewPix(20));
        _typeBtn2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    }
    return _typeBtn2;
}

-(UIButton *)typeBtn3{
    if(!_typeBtn3){
        _typeBtn3 = [UIButton buttonWithTitle:@" 实性结节" titleFont:LGFont(15) textColor:@"#333333" imageName:@"患者报告-PGGN2"];
        _typeBtn3.frame = CGRectMake(kMaxX(self.typeBtn2.frame)+viewPix(10), kMinY(self.typeBtn2.frame), (Screen_W-viewPix(32)-viewPix(30))/3.0, viewPix(20));
        _typeBtn3.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    }
    return _typeBtn3;
}

@end
